package cn.sh.pet.service.impl;

import cn.sh.basic.util.BaiduAuditUtils;
import cn.sh.basic.util.LoginContextUtils;
import cn.sh.org.domain.Employee;
import cn.sh.org.mapper.EmployeeMapper;
import cn.sh.pet.domain.Pet;
import cn.sh.pet.domain.PetOnlineAuditLog;
import cn.sh.pet.mapper.PetDetailMapper;
import cn.sh.pet.mapper.PetMapper;
import cn.sh.pet.mapper.PetOnlineAuditLogMapper;
import cn.sh.pet.service.IPetService;
import cn.sh.basic.service.impl.BaseServiceImpl;
import cn.sh.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sh
 * @since 2022-08-18
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void add(Pet pet) {
        //宠物初始化下架状态
        pet.setState(0);
        //宠物创建时间初始化
        pet.setCreatetime(new Date());
        //保存pet
        petMapper.add(pet);
        //保存detail
        pet.getDetail().setPetId(pet.getId());
        petDetailMapper.add(pet.getDetail());
    }

    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) {
        //获取当前登录用户信息
        Logininfo loginInfo = LoginContextUtils.getCurrentUser(request);
        //获取当前登录用户的员工信息
        Employee employee = employeeMapper.queryByLoginInfoId(loginInfo.getId());

        for (Long id : ids) {
            Boolean flag = true;
            //获取每一个pet
            Pet pet = petMapper.selectOne(id);
//            StringBuilder sb = new StringBuilder();
            //百度审核name，图片
            if (pet.getState() != 1) {
                if (!BaiduAuditUtils.TextCensor(pet.getName())) {
                    flag = false;
//                sb.append("宠物名称不合法");
                }
                //拿到图片的所有地址
                if (flag) {
                    String[] urls = pet.getResources().split(",");
                    if (urls.length > 0) {
                        //循环得到每一个图片地址
                        for (String url : urls) {
                            if (!BaiduAuditUtils.ImgCensor("http://123.207.27.208" + url)) {
//                        sb.append("宠物图片不合法");
//                        break;
                            }
                        }
                    }

                }

                if (flag) {
                    //保存成功审核日志
                    PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                    //设置为审核通过状态
                    petOnlineAuditLog.setState(1);
                    //设置审核通过时间
                    petOnlineAuditLog.setAuditTime(new Date());
                    //设置通过审核的宠物id
                    petOnlineAuditLog.setPetId(pet.getId());
                    //备注
                    petOnlineAuditLog.setNote("审核通过");
                    petOnlineAuditLog.setAuditId(employee.getId());
                    //保存日志
                    petOnlineAuditLogMapper.add(petOnlineAuditLog);
                    //更新宠物上架状态，上架时间
                    //宠物上架时间
                    pet.setOnsaletime(new Date());
                    //宠物状态
                    pet.setState(1);
                    //更新数据
                    petMapper.update(pet);

                } else { //审核未通过
                    //保存失败审核日志
                    PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                    //设置为审核通过状态
                    petOnlineAuditLog.setState(0);
                    //设置审核通过时间
                    petOnlineAuditLog.setAuditTime(new Date());
                    //设置通过审核的宠物id
                    petOnlineAuditLog.setPetId(pet.getId());
                    //备注
                    petOnlineAuditLog.setNote("审核失败");
                    //保存日志
                    petOnlineAuditLogMapper.add(petOnlineAuditLog);
                }
            }
        }
    }

    @Override
    public void offsale(List<Long> ids) {
        for (Long id : ids) {
            //查询每个宠物
            Pet pet = petMapper.selectOne(id);
            //标记
            Boolean f = true;
            //判断宠物是否下架
            //if(pet.getState() !=0){
            //查询到宠物日志
                /*PetOnlineAuditLog petOnlineAuditLog = petOnlineAuditLogMapper.selectOne(pet.getId());
                petOnlineAuditLog.setNote("下架");
                petOnlineAuditLog.setAuditTime(new Date());
                petOnlineAuditLog.setState(0);
                petOnlineAuditLogMapper.update(petOnlineAuditLog);*/
            //宠物下架时间
            pet.setOffsaletime(new Date());
            //宠物状态
            pet.setState(0);
            //更新数据
            petMapper.update(pet);
        }
    }


    @Override
    public Pet selectOne(Long id) {
        return super.selectOne(id);
    }


}
